<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // December 25, 2011
    // IN PARAMETERS : routeId, holderUserId
    // 
    // This AJAX Request identifies if the holder of the cancelled documents is the Archiver.

    include('../includes/siteConfig.php');
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // PASSED VARIABLES VIA POST
    $routeId = $_POST['routeId'];
    $holderUserId = $_POST['holderUserId'];
    
    // GET STEP ID FOR STEP NUMBER 1, CANCELTRAC of a particular ROUTE ID.
    $query = 
        "
            SELECT stepId
            FROM RouteStep
            WHERE routeId = '$routeId' AND subRouteId = 'CANCELTRAC' AND stepNumber = '1'
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
     
    // CANCEL TRACK STEP NUMBER 1 > $cancelStepId
    $row = mysql_fetch_array($result);
    $cancelStepId = $row['stepId'];
    
    // TIME TO CHECK IF THE HOLDER IS THE ARCHIVER OF THE DOCUMENT.
    $query = 
        "
            SELECT *
            FROM Account_Dept_Pos, RouteStep_Dept_Pos
            WHERE Account_Dept_Pos.userId = '$holderUserId' AND 
                  Account_Dept_Pos.deptPosId = RouteStep_Dept_Pos.deptPosId AND
                  RouteStep_Dept_Pos.stepId = '$cancelStepId'
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    // IF HOLDER = ARCHIVER
    if (mysql_num_rows($result) > 0) {
        echo json_encode(1);
    }
    
    // IF HOLDER != ARCHIVER
    else {
        echo json_encode(0);
    }
    mysql_close($connect);
    
   
?>
